﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ParsecParser;
using ParsecParser.Data;
using ParsecParser.Helpers;
using ParsecParser.Communication;
using ParsecParser.Display;
using ParsecClient.Classes;

namespace ParsecClient
{
    /// <summary>
    /// Interaction logic for RaidHps.xaml
    /// </summary>
    public partial class RaidTps : Window
    {
        public bool TankMode = true;
        private string CurrentCharacterName;
        private long CurrentThreat = 0;
        public RaidTps()
        {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, new ExecutedRoutedEventHandler(delegate(object sender, ExecutedRoutedEventArgs args) { this.Close(); })));
        }
        public void BindData(CombatDetails currentFight)
        {
            Topmost = true;
            if (currentFight == null)
            {
                CurrentCharacterName = null;
                CurrentThreat = 0;
                icRaidTps.ItemsSource = new List<ThreatDisplayItem>();
                txtTarget.Text = "";
            }
            else
            {
                CurrentCharacterName = currentFight.CharacterName;
                //find target
                CombatLogParticipant currentTarget = currentFight.LastOutgoingDamage != null ? currentFight.LastOutgoingDamage.Target : null;
                string currentTargetKey = null;
                if (currentTarget != null && !string.IsNullOrEmpty(currentTarget.DisplayName))
                {
                    txtTarget.Text = currentTarget.DisplayName;
                    currentTargetKey = currentTarget.GetUniqueKey();
                    if (currentTargetKey != null)
                    {
                        CombatLogParticipant currentParticipant = currentFight.GetParticipant(currentTargetKey);
                        if (currentParticipant != null)
                            CurrentThreat = currentParticipant.TotalThreat;
                        var raidDetails = currentFight.GetRaidDetails().Select(x => new ThreatDisplayItem(x.CharacterName, (x.GetParticipant(currentTargetKey) ?? new CombatLogParticipant()).TotalThreat)).Where(x => x.Threat > 0).OrderByDescending(x => x.Threat).ToList();
                        if (raidDetails != null && raidDetails.Count > 0)
                        {
                            var topThreat = raidDetails.First();
                            foreach (var r in raidDetails)
                            {
                                r.SetDisplayColor(TankMode, currentFight.Threat, currentFight.CharacterName, topThreat.Threat, topThreat.Name);
                            }
                            var displayList = raidDetails.Take(7).ToList();
                            if (displayList.FirstOrDefault(x => x.Name == CurrentCharacterName) == null)
                            {
                                var currentChar = raidDetails.FirstOrDefault(x => x.Name == CurrentCharacterName);
                                if (currentChar != null)
                                {
                                    displayList.Add(currentChar);
                                }
                            }
                            icRaidTps.ItemsSource = displayList;
                        }
                    }
                }
                else
                {
                    txtTarget.Text = "";
                    CurrentThreat = 0;
                    icRaidTps.ItemsSource = new List<ThreatDisplayItem>();
                }
            }
            if (TankMode)
                btnToggleMode.Content = "T";
            else
                btnToggleMode.Content = "D";
        }
        private void ReBindData()
        {
            if (icRaidTps.ItemsSource != null)
            {
                List<ThreatDisplayItem> threatList = (List<ThreatDisplayItem>)icRaidTps.ItemsSource;
                var topThreat = threatList.FirstOrDefault() ?? new ThreatDisplayItem();
                threatList = threatList.Select(x => new ThreatDisplayItem(TankMode, x.Name, x.Threat, CurrentThreat, CurrentCharacterName, topThreat.Threat, topThreat.Name)).ToList();
                icRaidTps.ItemsSource = threatList.Where(x => x.Threat > 0).OrderByDescending(x => x.Threat).Take(5).ToList();
            }
            if (TankMode)
                btnToggleMode.Content = "T";
            else
                btnToggleMode.Content = "D";
        }
        public double GetBackgroundOpacity()
        {
            return BorderBackground.Opacity;
        }
        public void SetBackgroundOpacity(double opacity)
        {
            BorderBackground.Opacity = opacity;
            BorderSolid.Opacity = opacity;
            BorderBlur.Opacity = opacity;
        }

        public void DragWindow(object sender, MouseButtonEventArgs args)
        {
            try
            {
                this.WindowState = WindowState.Normal;
                DragMove();
                args.Handled = true;
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out DragWindow", this.Title), ex, null, true);
            }
        }
        private void btnIncreaseOpacity_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BorderBackground.Opacity < 1)
                {
                    BorderBackground.Opacity += .1;
                    BorderSolid.Opacity += .1;
                    BorderBlur.Opacity += .1;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseOpacity_Click", this.Title), ex, null, true);
            }
        }

        private void btnDecreaseOpacity_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BorderBackground.Opacity > 0)
                {
                    BorderBackground.Opacity -= .1;
                    BorderSolid.Opacity -= .1;
                    BorderBlur.Opacity -= .1;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnDecreaseOpacity_Click", this.Title), ex, null, true);
            }
        }

        private void btnIncreaseSize_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Width < 1000)
                {
                    this.Width += 10;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnIncreaseSize_Click", this.Title), ex, null, true);
            }
        }

        private void btnDecreaseSize_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Width > 100)
                {
                    this.Width -= 10;
                }
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnDecreaseSize_Click", this.Title), ex, null, true);
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (Application.Current.MainWindow != null)
                {
                    ((ParsecWindow)Application.Current.MainWindow).btnRaidTps.IsChecked = false;
                }
                this.Hide();
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnClose_Click", this.Title), ex, null, true);
            }
        }

        private void btnToggleMode_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TankMode = !TankMode;
                ReBindData();
            }
            catch (Exception ex)
            {
                ((App)Application.Current).HandleError("Error", string.Format("Error on {0} Pop Out btnToggleMode_Click", this.Title), ex, null, true);
            }
        }
    }
}
